jQuery(document).ready(function() {
	
	
	initTable();
	App.setPage("ruleSup");  //Set current page
	App.init(); //Initialise plugins and elements
	bindItem("itemtypecode", null, null);
	
	
	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	$('#addRule').click(function(){
		 setModalSize();
		  $('#ifModal').attr("src", "add.html");
		  $('#modalTile').html("新增规则");
		  $('#modal').modal('show');
	});
	
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	
	
});
 



function initTable(){
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'supervise/ruleList.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        queryParams:function(params) {
        	var param ={};
        	param.pageNum =  params.offset / params.limit + 1;
        	param.pageSize = params.limit;
        	var itemNo = $('#itemtypecode').val();
        	var startTime = $('#starttime').val();
        	var endTime = $('#endtime').val();
        	if(""!= itemNo)
        		param.itemNo = itemNo;
        	if(""!= startTime)
            	param.startTime = startTime;
        	if(""!= endTime){
        		param.endTime = endTime;
        	}
            return param;
        },
	    columns: [{
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '50px',
            formatter : function(value, row, index) {  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'itemname',
	        title: '政务项目' 
	    }, {
	        field: 'remindtypename',
	        title: '提醒方式'
	    }, {
	        field: 'remindrule',
	        title: '提醒规则',
	        formatter : function(value, row, index) { 
            	 if('01'==row.remindruletype){
            		 return "距设定时限相差"+row.remindrule+"天";
            	 }else if('02' ==row.remindruletype){
            		 return "办理进度小于"+row.remindrule+"星";
            	 }else{
            		 return "服务态度小于"+row.remindrule+"星";
            	 }
		    }  
	    }, {
            field: 'remindprescription',
            title: '提醒时效(天)'
	    }, {
            field: 'remindrate',
            title: '提醒频率',
            formatter : function(value, row, index) { 
            	return row.remindrate+""+row.remindrateunitName;
		    } 
	    }, {
            field: 'status',
            title: '是否发布',
            formatter : function(value, row, index) { 
            	if(1==value){
            		return '已发布';
            	}else{
            		return  '<span style="color:red;">未发布</span>'; 
            	}
		       
		    } 
	    },  {
            field: 'op',
            title: '操作',
            events: operateEvents,
            formatter: operateFormatter
	    }] 
	});
}

function operateFormatter(value, row, index) {
    return [
    	  '<button type="button" class="Edit btn btn-warning  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
          '<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>'
		    ].join('');
    
}

window.operateEvents = {
  'click .Edit': function (e, value, row, index) {		 
	  setModalSize();
	  var ruleId = row.ruleid;
	  $('#ifModal').attr("src", "edit.html?ruleId="+ruleId);
  	
	  $('#modalTile').html("规则编辑");
	  $('#modal').modal('show');
  },
  'click .Delete':function(e,value,row,index){
		var ruleId = row.ruleid;
		window.wxc.xcConfirm('是否<strong>删除</strong>规则数据?<br />', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'supervise/delete.do',
					data: {"ruleId": ruleId},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});
  }
};